package tool;

import aboutbook.Book;
import aboutbook.BookList;

import java.util.Scanner;

public class Del implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("删除方法");
        Scanner myScanner = new Scanner(System.in);

        System.out.println("请输入你要删除的图书名字");
        String name = myScanner.nextLine();

        int currentnum = bookList.getBebooks();
        int pos = -1;
        int i = 0;
        for (; i < currentnum; i++) {
            Book book = bookList.getbooks(i);
            if(book.getName().equals(name)){
                //找到了 书 并 记录下标
                pos = i;
                break;
            }
        }

        if(i >= currentnum){
            System.out.println("没有你要删除的书");
            return;
        }

        //走到此处 开始删除
        for (int j = pos; j < currentnum - 1; j++) {
            // 本质思想是 bookList[j] = bookList[j+1]
            // 但 booklist 不是数组
            Book book = bookList.getbooks(j+1);
            bookList.setBooks(j,book);
        }

        bookList.setBebooks(currentnum - 1);





    }
}
